<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import jyglService from '@/api/jygl'

const emit = defineEmits(['refresh'])
/*
 * 打开弹窗
 */
//
const dialogVisible = ref(false)
const openDialog = (item: any) => {
  form.value.jysqbh = item.jysqbh
  form.value.jylcdm = item.jylcdm
  form.value.shjgdm = 'gd0470002'
  form.value.shjgmc = '退回'
  form.value.thyy = ''
  dialogVisible.value = true
}

/*
 * 确定按钮
 */
// 保存借阅审核结果入参
const form = ref({
  jysqbh: '', // 借阅申请数据编号
  jylcdm: '', // 借阅审核流程代码
  shjgdm: '', // 审核结果代码
  shjgmc: '', // 审核结果名称
  thyy: '', // 退回原因
})
const rules = {
  thyy: [
    {
      required: true,
      message: '请输入退回原因',
      trigger: 'blur',
    },
  ],
}
const ruleFormRef = ref()
const confirm = () => {
  const params = form.value
  ruleFormRef.value.validate((valid: boolean) => {
    if (valid) {
      jyglService
        .saveJyShjg(params)
        .then(() => {
          dialogVisible.value = false
          ElMessage({ type: 'success', message: '退回成功' })
          emit('refresh')
        })
        .catch((err) => {
          !err.isError && ElMessage({ type: 'warning', message: err.message })
        })
    }
  })
}

defineExpose({ openDialog })
</script>
<template>
  <el-dialog v-model="dialogVisible" title="退回" width="400" :close-on-click-modal="false">
    <div class="dialog-form">
      <el-form
        ref="ruleFormRef"
        :model="form"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="退回原因" prop="thyy">
          <el-input v-model="form.thyy" type="textarea" :rows="4" maxlength="100" show-word-limit />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" style="margin-left: 10px" @click="confirm">确 定</el-button>
        <el-button type="default" style="margin-left: 10px" @click="dialogVisible = false"
          >取 消</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped></style>
